<?php
/**
* @author Bimal Poudel
* @copyright since 2006, Bimal Poudel
* @package Smarty Framework
* @support http://www.odesk.com/users/~~dd91d11caed0cdce
* @contact http://www.sanjaal.com
* @company Sanjaal Corps
*/

# Created on: 2012-04-08 12:26:21 248

/**
* Edit an entity in [ pokes ]
*/

$pokes = new pokes();

# Handle Editing, when data is supplied
if($variable->post('edit-action', 'string', '') && ($collection_id = $variable->post('collection_id', 'integer', 0)))
{
	# Refer to old records in case we need it
	$old = $pokes->details($collection_id);
	if(!$old)
	{
		$messenger = new messenger('success', 'The record does not exist.');
		stopper::url('pokes-edit-error.php?context=nodata');
	}
	
	# Editing....
	$code = $variable->post('protection_code', 'string', '');
	$data = $variable->post('pokes', 'array', array());
	
	# Mark when this data was modified last time.
	$data['modified_on'] = 'UNIX_TIMESTAMP(CURRENT_TIMESTAMP())';
	$data['modified_counter'] = 'modified_counter+1';
	
	# If there are some FILEs to upload, please do now.
	# And assign the name of the file returned, to the same field.
	# Old uploaded file should be probably lost. So, delete it first.
	# $uploader = new uploader(__BASE__.'/templates/images/pokes', true);
	# #$data['file_field'] = uploader::upload_delete($collection_id);
	# $file_field = $uploader->store('file_field', '');
	# if($file_field) $data['file_field'] = $file_field;
	
	# Produce the cropnail for uploaded images, if any. Adjust the dimensions as well.
	# $original_image = __BASE__.'/templates/images/pokes/'.$data['file_field'];
	# $cropnail_image = __BASE__.'/templates/images/pokes/thumbs/'.$data['file_field'];
	# $cropnail = new cropnail(200, 150); $cropnail->resize($original_image, $cropnail_image, 0);
	
	if($success = $pokes->edit(
		$data, # Posted data
		array(
			'collection_id' => $collection_id,
		),
		$code, # Security code related to this entry
		$collection_id
	))
	{
		# Something about the image uploaders as a patch, if applies
		# $cu = new customized_uploader('uploader', __BASE__.'/templates/images/pokes', 'images/pokes', $record_id=$collection_id);
		
		/*# Optionally remove old uploaded file, if any
		if($old['file_field'])
		{
			$file_field = __BASE__.'/templates/images/pokes/'.$old['file_field'];
			if(is_file($file_field))
			{
				unlink($file_field);
			}
		}*/
		
		$messenger = new messenger('success', 'The record has been modified.');
		
		stopper::url(url::last_page('pokes-list.php?context=permissions'));
		#stopper::url('pokes-edit-successful.php');
		#stopper::url('pokes-list.php');
	}
	else
	{
		stopper::url('pokes-edit-error.php');
	}
}
else
{
	/**
	* Otherwise, load the details of the entity before editing it.
	*/
	if($collection_id = $variable->get('id', 'integer', 0))
	{
		$details = $pokes->details($collection_id);
		if(!$details)
		{
			# Data about this entity was not available
			stopper::url('pokes-edit-error.php?context=data');
		}
		
		if($details['code'] != $variable->get('code', 'string', ''))
		{
			$messenger = new messenger('error', 'You are attempting to edit wrong data.');
			stopper::url('pokes-edit-error.php?context=attemptedwrongdata');
		}
		
		# Purpose of this code block is to make sure that the variable
		# gets all indices with blank data, to feed to EDIT form.
		$details = $pokes->validate('edit', $details);

		/**
		* Build Smarty Variable with FULL details
		*/
		$smarty->assign('pokes', $details);
	}
	else
	{
		# Really Bad...
		stopper::url('pokes-direct-access-error.php');
	}
}
?>